package models.admin;

import models.BaseModel;
import models.admin.enums.AdminUserAuth;
import models.admin.enums.AdminUserType;
import models.constants.AvailableStatus;
import models.constants.DeletedStatus;
import models.constants.Gender;
import models.system.Company;
import models.user.User;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * Created by liming on 16/6/29.
 */
@Entity
@Table(name = "admin_user")
public class AdminUser extends BaseModel {
    public static String LOGIN_TAG = "admin_tag";
    /**
     * 登陆名
     */
    @Column(name = "login_name")
    public String loginName;

    /**
     * 名称
     */
    @Column(name = "name")
    public String name;

    /**
     * 密码
     */
    @Column(name = "password")
    public String password;

    /**
     * 所属机构
     */
    @JoinColumn(name = "organization")
    @ManyToOne
    public Company organization;

    /**
     * 性别 0 男, 1 女, 2 未知
     */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "gender")
    public Gender gender;

    /**
     * 年龄
     */
    @Column(name = "age")
    public Integer age;

    /**
     * 职位信息.
     */
    @Column(name = "position", length = 64)
    public String position;

    /**
     * 联系电话
     */
    @Column(name = "email")
    public String email;

    /**
     * 联系电话
     */
    @Column(name = "phone_no")
    public String phoneNo;

    /**
     * 微信号.
     *
     * 企业内必须唯一。（注意：是微信号，不是微信的名字）
     */
    @Column(name = "weixin_id", length = 64)
    public String weixinId;

    /**
     * 企业号的UserId
     * 企业号唯一
     */
    @Column(name = "cp_user_id")
    public String cpUserId;

    /**
     * 是否是管理员
     */
    @Column(name = "is_admin")
    public Boolean isAdmin;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;

    /**
     * 绑定微信用户ID
     */
    @JoinColumn(name = "weixin_user_id")
    @ManyToOne
    public User weiXinUser;

    /**
     * 隶属部门
     */
    @ManyToOne
    @JoinColumn(name = "admin_dept_id")
    public AdminDept adminDept;

    /**
     * 适用状态
     */
    @Column(name = "available_status")
    @Enumerated(EnumType.STRING)
    public AvailableStatus availableStatus;

    /**
     * 用户类型
     */
    @Column(name = "admin_user_type")
    @Enumerated(EnumType.STRING)
    public AdminUserType adminUserType;

    /**
     * 用户权限类型
     */
    @Column(name = "admin_user_auth")
    @Enumerated(EnumType.STRING)
    public AdminUserAuth adminUserAuth;

    /**
     * 外协公司名称
     */
    @Column(name = "outsourcing_company")
    public String outsourcingCompany;


    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "deleted")
    public DeletedStatus deleted = DeletedStatus.UN_DELETED;

    /**
     * 创建人Id
     */
    @Column(name = "ct_user_id")
    public Long ctUserId;

    /**
     * 上次登录时间
     */
    @Column(name = "last_login_time")
    public Date lastLoginTime;

    /**
     * 通过id查询用户
     */
    public static AdminUser findAdminUserById(long id) {
        return AdminUser.find("id=? and deleted=?", id, DeletedStatus.UN_DELETED).first();
    }

    public static long findRepeatLoginName(long id,String loginName) {
        return AdminUser.count("id != ? and deleted = ? and loginName = ?",id,DeletedStatus.UN_DELETED,loginName);
    }

    /**
     * 通过登录名查找用户
     * @param loginName
     * @return
     */
    public static AdminUser findAdminUserByLoginName(String loginName) {
        return AdminUser.find("loginName = ? and deleted = ?",loginName,DeletedStatus.UN_DELETED).first();
    }

    public static AdminUser findAdminUserBycpUserIdAndComp(String userId,Company company) {
        return AdminUser.find("cpUserId = ? and organization = ? and deleted = ?",userId,company,DeletedStatus.UN_DELETED).first();
    }


    /**
     * 通过用户类型查询用户
     * @param adminUserType
     */
    public static AdminUser findAdminUserByType(AdminUserType adminUserType) {
        return AdminUser.find("adminUserType = ? and deleted = ?",adminUserType,DeletedStatus.UN_DELETED).first();
    }

    public static List<AdminUser> findAllAdminUserByTypeAndCompany(AdminUserType adminUserType,Company company) {
        if(company == null) {
            return AdminUser.find("adminUserType = ? and deleted = ?",adminUserType,DeletedStatus.UN_DELETED).fetch();
        }else {
            return AdminUser.find("adminUserType = ? and organization = ? and deleted = ?",adminUserType,company,DeletedStatus.UN_DELETED).fetch();
        }
    }

    public static void execNativeSql(String sql) {
        Query query = em().createNativeQuery(sql);
        query.executeUpdate();
    }
}
